package com.datatools.util;

import java.io.*;

public class SerializationUtil {

    private static String path = System.getProperty("user.dir")+File.separator+".DataTools";
    /**
     * 序列化
     */
    public static void writeObject(Serializable s,String clazzName) {
        try {
            String serializPath = path +File.separator+clazzName;
            System.out.println("序列化writeObject输出:"+serializPath);
            File writeFile = new File(serializPath);
            if(writeFile.exists()==false) {
                File parentDir = writeFile.getParentFile();
                if(parentDir.exists()==false){
                    parentDir.mkdir();
                }
                writeFile.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(writeFile);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(s);
            oos.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反序列化
     */
    public static Object readObject(String clazzName) {
        Object obj = null;
        try {
            String serializPath = path +File.separator+clazzName;
            System.out.println("反序列化 readObject :"+serializPath);
            File readFile = new File(serializPath);
            if(readFile.exists()) {
                FileInputStream fis = new FileInputStream(readFile);
                ObjectInput input = new ObjectInputStream(fis);
                obj = input.readObject();
                input.close();
                fis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
}
